import org.newdawn.slick.*;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Handles loading resources and storing them for use.
 * 
 * @author Gerrit Jamerson, CodeMonkeys
 * @version 4/3/13
 */
public class GameRegistry
{
    private static ArrayList<SpriteSheetInfo> listSheets = new ArrayList<SpriteSheetInfo>(); //List of all loaded sprite sheets
    private static ArrayList<ImageInfo> listImages = new ArrayList<ImageInfo>(); //List of all loaded images
    private static ArrayList<SoundInfo> listSounds = new ArrayList<SoundInfo>(); //List of all loaded sounds
    private static ArrayList<FontInfo> listFonts = new ArrayList<FontInfo>(); //List of all loaded fonts

    /**
     * Loads the image at the given path for later use
     * @pre Image file must exist
     * @param strPath The path to the file
     * @param strImageName The name to use when getting the image
     * @return void
     * @post The image has been loaded into memory/stored into listImages
     */
    public static void loadFont(String strPath, String strFontName)
    {
        try
        {
            try
            {
                File fontFile = new File(strPath);
                TrueTypeFont ttf = new TrueTypeFont(Font.createFont(Font.TRUETYPE_FONT, fontFile), false);

                listFonts.add(new FontInfo(ttf, strFontName));
                System.out.println("[GR] Loaded font " + strFontName);
            } //End try
            catch (FontFormatException e)
            {
                System.out.println(e.getMessage());    
            } //End catch
        } //End try
        catch (IOException e)
        {
            System.out.println(e.getMessage());    
        } //End catch
    } //End loadFont method

    /**
     * Loads the image at the given path for later use
     * @pre Image file must exist
     * @param strPath The path to the file
     * @param strImageName The name to use when getting the image
     * @param fltFontSize The size of the font.
     * @return void
     * @post The image has been loaded into memory/stored into listImages
     */
    public static void loadFont(String strPath, String strFontName, float fltFontSize, boolean antiAlias)
    {    
        try
        {
            try
            {
                File fontFile = new File(strPath);
                TrueTypeFont ttf = new TrueTypeFont(Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(fltFontSize), antiAlias);

                listFonts.add(new FontInfo(ttf, strFontName));
                System.out.println("[GR] Loaded font " + strFontName);
            } //End try
            catch (FontFormatException e)
            {
                System.out.println(e.getMessage());    
            } //End catch
        } //End try
        catch (IOException e)
        {
            System.out.println(e.getMessage());    
        } //End catch
    }

    /**
     * Loads the image at the given path for later use
     * @pre Image file must exist
     * @param strPath The path to the file
     * @param strImageName The name to use when getting the image
     * @return void
     * @post The image has been loaded into memory/stored into listImages
     */
    public static void loadImage(String strPath, String strImageName)
    {
        try
        {
            Image image = new Image(strPath, false, Image.FILTER_NEAREST);
            listImages.add(new ImageInfo(image, strImageName));
            System.out.println("[GR] Loaded image " + strImageName);
        } //End try
        catch (SlickException e)
        {
            System.out.println(e.getMessage());    
        } //End catch
    } //End loadImage method

    /**
     * Loads the sprite sheet at the given path for later use
     * @pre Sprite sheet file must exist
     * @param strPath The path to the file
     * @param strImageName The name to use when getting the sprite sheet
     * @return void
     * @post The sprite sheet has been loaded into memory/stored into listSheets
     */
    public static void loadSpriteSheet(String strPath, String strSheetName, int tileWidth, int tileHeight)
    {
        try
        {
            Image image = new Image(strPath, false, Image.FILTER_NEAREST);
            SpriteSheet spriteSheet = new SpriteSheet(image, tileWidth, tileHeight);
            listSheets.add(new SpriteSheetInfo(spriteSheet, strSheetName));
            System.out.println("[GR] Loaded image " + strSheetName);
        } //End try
        catch (SlickException e)
        {
            System.out.println(e.getMessage());    
        } //End catch
    } //End loadSpriteSheet method

    /**
     * Loads the sound at the given path for later use
     * @pre Sound file must exist
     * @param strPath The path to the file
     * @param strSoundName The name to use when getting the sound
     * @return void
     * @post The sound has been loaded into memory/stored into listSounds
     */
    public static void loadSound(String strPath, String strSoundName) { }

    /**
     * Returns the font with the given image name
     * @pre The font must have been loaded using loadImage()
     * @param strFontName The name of the image
     * @return The font (or null if it doesn't exist)
     * @post none
     */
    public static TrueTypeFont getFont(String strFontName)
    {
        for(FontInfo fontInfo : listFonts)
        {
            if(fontInfo.getName() == strFontName)
            {
                return fontInfo.getFont();
            } //End if
        } //End foreach loop

        return null;
    } //End getFont method

    /**
     * Returns the image with the given image name
     * @pre The image must have been loaded using loadImage()
     * @param strImageName The name of the image
     * @return The image (or null if it doesn't exist)
     * @post none
     */
    public static Image getImage(String strImageName)
    {
        for(ImageInfo info : listImages)
        {
            if(info.getName() == strImageName)
            {
                return info.getImage();
            } //End if
        } //End foreach loop

        return null;
    } //End getImage method

    /**
     * Returns the sprite sheet with the given sheet name
     * @pre The sheet must have been loaded using loadSpriteSheet()
     * @param strSheetName The name of the sheet
     * @return The sprite sheet (or null if it doesn't exist)
     * @post none
     */
    public static SpriteSheet getSpriteSheet(String strSheetName)
    {
        for(SpriteSheetInfo info : listSheets)
        {
            if(info.getName() == strSheetName)
            {
                return info.getSpriteSheet();
            } //End if
        } //End foreach loop

        return null;
    } //End getSpriteSheet method
    
    /**
     * Returns the sound with the given image name
     * @pre The sound must have been loaded using loadSound()
     * @param strSoundName The name of the sound
     * @return The sound (or null if it doesnt exist)
     * @post none
     */
    public static Sound getSound(String strSoundName)
    {
        return null;
    } //End getSound method
} //End GameRegistry class
